package dragon.modules.system.user.dao;

import java.util.ArrayList;
import java.util.List;

import dragon.core.db.DML;
import dragon.core.exception.ExceptionHandler;
import dragon.core.system.po.SysUser;

public class UserDaoImpl {

	/**
	 * 获取所有用户
	 * 
	 * @return
	 * @throws ExceptionHandler
	 */
	public List<SysUser> loadAllUser() throws ExceptionHandler {
		return DML.loadObjects(new ArrayList<SysUser>(), SysUser.class);
	}

	/**
	 * 根据id数组获取指定用户
	 * 
	 * @param ids
	 * @return
	 * @throws ExceptionHandler
	 */
	public List<SysUser> loadNotMarkUser(int[] ids) throws ExceptionHandler {
		StringBuffer sql = new StringBuffer();
		sql.append("select * from ").append(SysUser.tableName).append(" where ");
		for (int i : ids) {
			sql.append("id!=" + i + " and ");
		}
		sql.replace(sql.lastIndexOf(" and "), sql.length(), "");
		return DML.query(new ArrayList<SysUser>(), SysUser.class, sql.toString());
	}

	/**
	 * 更新用户，视null为不更新(用户org关系不在此处更新)
	 * 
	 * @param user
	 * @return
	 * @throws ExceptionHandler
	 */
	public boolean updataUser(SysUser user) throws ExceptionHandler {
		SysUser u = DML.loadObject(SysUser.class, "where id=?", user.getId());
		if (u != null) {
			if (user.getEmail() != null) {
				u.setEmail(user.getEmail());
			}
			if (user.getPhone() != null) {
				u.setPhone(user.getPhone());
			}
			if (user.getLastloginip() != null) {
				u.setLastloginip(user.getLastloginip());
			}
			if (user.getLastlogintime() != null) {
				u.setLastlogintime(user.getLastlogintime());
			}
			if (user.getLoginname() != null) {
				u.setLoginname(user.getLoginname());
			}
			if (user.getPwd() != null) {
				u.setPwd(user.getPwd());
			}
			if (user.getState() != null) {
				u.setState(user.getState());
			}
			if (user.getUsername() != null) {
				u.setUsername(user.getUsername());
			}
			return DML.saveOrUpdateObject(u) > 0 ? true : false;
		} else {
			return false;
		}
	}

	/**
	 * 根据id获取用户
	 * 
	 * @param id
	 * @return
	 * @throws ExceptionHandler
	 */
	public SysUser getUser(int id) throws ExceptionHandler {
		return DML.loadObject(SysUser.class, "where id=?", id);
	}

	/**
	 * 有且紧对用户的组织关系维护string视null不更新 int视-1为不更新
	 * 
	 * @param user
	 * @return
	 * @throws ExceptionHandler
	 */
	public boolean updateUserOrg(SysUser user) throws ExceptionHandler {
		SysUser u = DML.loadObject(SysUser.class, "where id=?", user.getId());
		if (u != null) {
			u.setOrg(user.getOrg());
			if (user.getOugroup() != null) {
				u.setOugroup(user.getOugroup());
			}
			return DML.saveOrUpdateObject(u) > 0 ? true : false;
		} else {
			return false;
		}
	}

	/**
	 * 根据Ids如（1,2,3）获取用户
	 * 
	 * @param user
	 * @return
	 * @throws ExceptionHandler
	 */
	public List<SysUser> getUsersByIds(String ids) throws ExceptionHandler {
		String sql = "select * from " + SysUser.tableName + " where id in (" + ids + ")";
		return DML.query(new ArrayList<SysUser>(), SysUser.class, sql);
	}

	/**
	 * 根据ids批改变用户状态
	 * 
	 * @param ids
	 * @param state
	 * @return
	 * @throws ExceptionHandler
	 */
	public int changeUsersState(String ids, String state) throws ExceptionHandler {
		return DML.executeUpdate("update " + SysUser.tableName + " set state='" + state + "' where id in (" + ids + ")");
	}

}
